home *** CD-ROM | disk | FTP | other *** search
/ Power Programmierung 2 / Power-Programmierung CD 2 (Tewi)(1994).iso / gnu / gnulib / ohlutil / msd_pwd.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-08-24  |  4.1 KB  |  201 lines

  1. /*  msd_pwd.c - Try to approximate UN*X's getuser...() functions under MS-DOS.
  2.     Copyright (C) 1990 by Thorsten Ohl, td12@ddagsi3.bitnet
  3.  
  4.     This program is free software; you can redistribute it and/or modify
  5.     it under the terms of the GNU General Public License as published by
  6.     the Free Software Foundation; either version 1, or (at your option)
  7.     any later version.
  8.  
  9.     This program is distributed in the hope that it will be useful,
  10.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.     GNU General Public License for more details.
  13.  
  14.     You should have received a copy of the GNU General Public License
  15.     along with this program; if not, write to the Free Software
  16.     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  17.  
  18.     IMPORTANT:
  19.  
  20.     This code is not an official part of the GNU project and the
  21.     author is not affiliated to the Free Software Foundation.
  22.     He just likes their code and spirit.
  23.  
  24.     $Header: e:/gnu/fileutil/RCS/msd_pwd.c'v 2.0 90/06/29 00:35:37 tho Stable $
  25. */
  26.  
  27. /* This 'implementation' is conjectured from the use of this functions in
  28.    the RCS and BASH distributions.  Of course these functions don't do too
  29.    much useful things under MS-DOS, but using them avoids many "#ifdef
  30.    MSDOS" in ported UN*X code ...  */
  31.  
  32.  
  33. #include <stdio.h>
  34. #include <stdlib.h>
  35. #include <string.h>
  36. #include "msd_pwd.h"
  37.  
  38. static char *lookup_env (char **);
  39.  
  40. /* where people might scribble their name into the environment ... */
  41.  
  42. static char *login_strings[] =
  43. {
  44.   "LOGIN", "UID", "USER", "MAILNAME", (char *) 0
  45. };
  46.  
  47. static char *group_strings[] =
  48. {
  49.   "GID", "GROUP", (char *) 0
  50. };
  51.  
  52.  
  53. static char *anonymous = "anonymous";    /* if all else fails ... */
  54.  
  55. static char *home_dir = ".";    /* we feel (no|every)where at home */
  56. static char *login_shell = "not command.com!";
  57.  
  58. static char *login = (char *) 0;/* cache the names here    */
  59. static char *group = (char *) 0;
  60.  
  61. static struct passwd pw;    /* should we return a malloc()'d structure   */
  62. static struct group gr;        /* instead of pointers to static structures? */
  63.  
  64. /* return something like a username in a (butchered!) passwd structure. */
  65. struct passwd *
  66. getpwuid (int uid)
  67. {
  68.   pw.pw_name = getlogin ();
  69.   pw.pw_dir = home_dir;
  70.   pw.pw_shell = login_shell;
  71.  
  72.   return &pw;
  73. }
  74.  
  75. struct passwd *
  76. getpwnam (char *name)
  77. {
  78.   return (struct passwd *) 0;
  79. }
  80.  
  81. /* return something like a groupname in a (butchered!) group structure. */
  82. struct group *
  83. getgrgid (int uid)
  84. {
  85.   gr.gr_name = getgr_name ();
  86.  
  87.   return &gr;
  88. }
  89.  
  90. /* return something like a username. */
  91. char *
  92. getlogin ()
  93. {
  94.   if (!login)            /* have we been called before? */
  95.     login = lookup_env (login_strings);
  96.  
  97.   if (!login)            /* have we been successful? */
  98.     login = anonymous;
  99.  
  100.   return login;
  101. }
  102.  
  103. /* return something like a group.  */
  104. char *
  105. getgr_name ()
  106. {
  107.   if (!group)            /* have we been called before? */
  108.     group = lookup_env (group_strings);
  109.  
  110.   if (!group)            /* have we been successful? */
  111.     group = anonymous;
  112.  
  113.   return group;
  114. }
  115.  
  116. /* return something like a uid.  */
  117. int
  118. getuid ()
  119. {
  120.   return 0;            /* every user is a super user ... */
  121. }
  122.  
  123. int
  124. getgid ()
  125. {
  126.   return 0;
  127. }
  128.  
  129. int
  130. geteuid ()
  131. {
  132.   return 0;
  133. }
  134.  
  135. int
  136. getegid ()
  137. {
  138.   return 0;
  139. }
  140.  
  141. struct passwd *
  142. getpwent ()
  143. {
  144.   return (struct passwd *) 0;
  145. }
  146.  
  147. void
  148. setpwent ()
  149. {
  150. }
  151.  
  152. void
  153. endpwent ()
  154. {
  155. }
  156.  
  157.  
  158. /* return groups.  */
  159. int
  160. getgroups (int ngroups, int *groups)
  161. {
  162.   *groups = 0;
  163.   return 1;
  164. }
  165.  
  166. /* lookup environment.  */
  167. static char *
  168. lookup_env (char *table[])
  169. {
  170.   char *ptr;
  171.   char *entry;
  172.   size_t len;
  173.  
  174.   while (*table && !(ptr = getenv (*table++))) ;    /* scan table */
  175.  
  176.   if (!ptr)
  177.     return (char *) 0;
  178.  
  179.   len = strcspn (ptr, " \n\t\n\r");    /* any WS?       */
  180.   if (!(entry = malloc (len + 1)))
  181.     {
  182.       fprintf (stderr, "Out of memory.\nStop.");
  183.       exit (-1);
  184.     }
  185.  
  186.   strncpy (entry, ptr, len);
  187.   entry[len] = '\0';
  188.  
  189.   return entry;
  190.  
  191. }
  192.  
  193.  
  194. /*
  195.  * Local Variables:
  196.  * mode:C
  197.  * ChangeLog:ChangeLog
  198.  * compile-command:make
  199.  * End:
  200.  */
  201.